package com.hl.payServer.service.impl;

import com.hl.payServer.mapper.PayPropertyMapper;
import com.hl.payServer.model.PayProperty;
import com.hl.payServer.model.ResultModel;
import com.hl.payServer.service.PayPropertyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import java.util.HashMap;
import java.util.List;


@Service
public class PayPropertyServiceImpl implements PayPropertyService {

    @Autowired
    private PayPropertyMapper payPropertyMapper;

    @Override
    public List<HashMap<String,Object>>  queryList(PayProperty entity) {
        HashMap<String, Object> param = new HashMap<String, Object>();
        param.put("companyCode", entity.getCompanyCode());
        return payPropertyMapper.selectByParam(param);
    }

    @Override
    public ResultModel save(PayProperty entity) {
        Example example = new Example(PayProperty.class);
        Example.Criteria criteria1 = example.createCriteria();
        criteria1.orEqualTo("companyCode", entity.getCompanyCode());
        criteria1.orEqualTo("companyName", entity.getCompanyName());
        Example.Criteria criteria2 = example.createCriteria();
        criteria2.andEqualTo("payChannle", entity.getPayChannle());
        criteria2.andEqualTo("isDelete", 0);
        criteria2.andEqualTo("payType", entity.getPayType());
        example.and(criteria2);
        List<PayProperty> list = payPropertyMapper.selectByExample(example);
        if (list != null && list.size() > 0) {
            return new ResultModel(false,"名称或编码重复,本次操作失败!");
        }
        payPropertyMapper.insertSelective(entity);
        return ResultModel.ok(entity);
    }

    @Override
    public ResultModel update(PayProperty entity) {
        Example example = new Example(PayProperty.class);
        Example.Criteria criteria1 = example.createCriteria();
        criteria1.orEqualTo("companyCode", entity.getCompanyCode());
        criteria1.orEqualTo("companyName", entity.getCompanyName());
        Example.Criteria criteria2 = example.createCriteria();
        criteria2.andEqualTo("payChannle", entity.getPayChannle());
        criteria2.andEqualTo("isDelete", 0);
        criteria2.andEqualTo("payType", entity.getPayType());
        criteria2.andNotEqualTo("id", entity.getId());
        example.and(criteria2);
        List<PayProperty> list = payPropertyMapper.selectByExample(example);
        if (list != null && list.size() > 0) {
            return new ResultModel(false,"名称或编码重复,本次操作失败!");
        }
        payPropertyMapper.updateByPrimaryKeySelective(entity);
        return ResultModel.ok(entity);
    }

    @Override
    public ResultModel delete(String id) {
        if (id != null) {
            PayProperty payProperty = new PayProperty();
            payProperty.setId(id);
            payProperty.setIsDelete(1);
            payPropertyMapper.updateByPrimaryKeySelective(payProperty);
        }
        return ResultModel.ok();
    }

    @Override
    public PayProperty selectByCompanyCodeAndPayChannle(String companyCode, Integer payChannle, String payType) {
        Example example = new Example(PayProperty.class);
        Example.Criteria criteria1 = example.createCriteria();
        criteria1.andEqualTo("companyCode", companyCode);
        criteria1.andEqualTo("payChannle", payChannle);
        criteria1.andEqualTo("payType", payType==null?"base":payType);
        criteria1.andEqualTo("isDelete", 0);
        return payPropertyMapper.selectOneByExample(example);
//        List<PayProperty> list = payPropertyMapper.selectByExample(example);
//        if (list != null && list.size() > 0) {
//            return list.get(0);
//        }
//        return null;
    }


}
